<template>
  <div class="page-box">
    <!-- 搜索区域 -->
    <el-collapse-transition>
      <div v-show="showSearchBox" class="search-box">
        <!-- :inline="true" 表单在一行显示 -->
        <el-form :inline="true" class="demo-form-inline">
          <el-form-item label="分类名称">
            <el-input v-model.trim="queryParams.catename" placeholder="请输入分类名称" />
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="search">搜索</el-button>
            <el-button icon="el-icon-refresh" @click="reset">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-collapse-transition>
    <!-- 工具条区域 -->
    <div class="toolbar-box">
      <el-row type="flex" justify="space-between">
        <div>
          <el-button plain type="primary" icon="el-icon-plus" @click="showDialog = true">新增</el-button>
          <el-button plain type="warning" icon="el-icon-download">导出</el-button>
        </div>
        <div>
          <el-tooltip content="关闭搜索区" placement="top">
            <el-button icon="el-icon-arrow-up" circle @click="showSearchBox = !showSearchBox" />
          </el-tooltip>
          <el-tooltip content="刷新" placement="top">
            <el-button icon="el-icon-refresh" circle @click="refresh" />
          </el-tooltip>
          <el-tooltip content="筛选" placement="top">
            <el-button icon="el-icon-menu" circle />
          </el-tooltip>
        </div>
      </el-row>
    </div>
    <!-- 表格区域 -->
    <div class="table-box">
      <el-table
        :data="list"
        style="width: 100%"
        border
      >
        <el-table-column type="index" label="#" width="50" />
        <el-table-column prop="id" label="分类ID" />
        <el-table-column prop="catename" label="分类名称" />
        <el-table-column prop="icon" label="图标class">
          <template #default="{row}">
            {{ row.icon }} <i class="fa" :class="row.icon" />
          </template>
        </el-table-column>
        <el-table-column prop="sort_num" label="权重" />
        <el-table-column label="操作" align="center">
          <template #default="{row}">
            <el-button type="primary" circle icon="el-icon-edit" @click="editCate(row.id)" />
            <el-button type="danger" circle icon="el-icon-delete" @click="doDel(row.id)" />
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页区域 -->
    <div class="pagination-box">
      <el-row type="flex" justify="end">
        <el-pagination
          background
          :current-page="queryParams.page"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="queryParams.limit"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </el-row>
    </div>
    <!-- 新增和编辑弹出对话框 -->
    <el-dialog :title="dialogTitle" :visible.sync="showDialog" width="40%" @closed="resetForm">
      <el-form ref="form" label-width="120px" :model="formData" :rules="rules">
        <el-form-item label="分类名称" prop="catename">
          <el-input v-model="formData.catename" style="width:400px" />
        </el-form-item>
        <el-form-item label="图标clss" prop="icon">
          <el-input v-model="formData.icon" style="width:200px;marginRight:20px" />
          <el-link href="https://fontawesome.com.cn/v4/icons-ui/" target="_blank">具体参见fontawesome图标库</el-link>
        </el-form-item>
        <el-form-item label="排序大小" prop="sort_num">
          <el-input-number v-model="formData.sort_num" :min="1" :max="100" placeholder="10" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="showDialog = false">取消</el-button>
        <el-button type="primary" @click="submit">确认</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { addCate, delCate, getCateDetail, getCateList, updateCate } from '@/api/content/cate'
export default {
  data() {
    return {
      list: [],
      queryParams: {
        catename: '', // 双向绑定搜索框
        page: 1,
        limit: 10
      },
      total: 0, // 数据总量
      showSearchBox: true, // 控制是否显示搜索区
      showDialog: false, // 控制新增|编辑对话框的打开和关闭
      formData: {
        catename: '',
        icon: '',
        sort_num: 10
      },
      rules: {
        catename: [{ required: true, message: '请输入分类名称', trigger: 'blur' }],
        icon: [{ required: true, message: '请输入图标class', trigger: 'blur' }]
      }
    }
  },
  computed: {
    // 处理对话框标题
    dialogTitle() {
      return this.formData.id ? '编辑分类' : '新增分类'
    }
  },
  watch: {
    'formData.sort_num'(newValue, oldValue) {
      if (!newValue) {
        this.formData.sort_num = 10
      }
    }
  },
  created() {
    this.initData()
  },
  methods: {
    // 初始渲染
    async initData() {
      const res = await getCateList(this.queryParams)
      this.list = res.data
      this.total = res.count
    },
    // 搜索
    search() {
      this.queryParams.page = 1
      this.initData()
    },
    // 重置搜索
    reset() {
      this.queryParams = {
        catename: '',
        page: 1,
        limit: 10
      }
      this.initData()
    },
    // 切换分页时触发
    handleCurrentChange(value) {
      this.queryParams.page = value
      this.initData()
    },
    // 切换每页显示条数时触发
    handleSizeChange(value) {
      // 切换时后到第一页
      this.queryParams.page = 1
      this.queryParams.limit = value
      this.initData()
    },
    // 刷新表格
    refresh() {
      this.initData()
    },
    // 提交表单(编辑|添加)
    submit() {
      this.$refs.form.validate(async isOK => {
        if (!isOK) return
        // 根据formData中是否有id判断是编辑还是新增操作
        await this.formData.id ? this.doEdit() : this.doAdd()
        this.showDialog = false
      })
    },
    // 新增操作
    async doAdd() {
      await addCate(this.formData)
      this.$message.success('新增分类成功！')
      this.queryParams.page = 1
      this.initData()
    },
    // 点击编辑按钮回显数据
    async editCate(id) {
      const data = await getCateDetail(id)
      this.formData = data
      if (!data.id) {
        this.$message.warning('该分类已被删除！')
        this.initData()
      } else {
        this.showDialog = true
      }
    },
    // 编辑操作
    async doEdit() {
      await updateCate(this.formData)
      this.$message.success('修改分类成功！')
      this.queryParams.page = 1
      this.initData()
    },
    // 删除操作
    doDel(id) {
      this.$confirm('确认删除该分类?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delCate(id)
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        // 处理删除当前页的最后一条
        if (this.list.length === 1 && this.queryParams.page > 1) {
          this.queryParams.page--
        }
        this.initData()
      }).catch(() => {
        return false
      })
    },
    // 关闭对话框后重置表单
    resetForm() {
      // 重置表单数据
      this.formData = {
        catename: '',
        icon: '',
        sort_num: 10
      }
      // 重置表单校验结果
      this.$refs.form.clearValidate()
    }
  }
}
</script>
<style lang="scss" scoped>
  .el-dialog__body{
    padding: 15px,20px;
  }
</style>
